package office.action.business;

import java.util.List;

import office.action.business.form.ContractConditionForm;
import office.action.business.form.ContractForm;
import office.action.business.form.GoodsOrderConditionForm;
import office.action.business.form.GoodsOrderDetailConditionForm;
import office.action.business.form.GoodsOrderForm;
import office.action.business.wrapper.ContractWrapper;
import office.action.business.wrapper.GoodsOrderWrapper;
import office.bo.business.impl.GoodsOrderDetail;
import office.service.business.IContractService;
import office.service.business.IGoodsOrderDetailService;
import office.service.business.IGoodsOrderService;
import platform.common.action.BaseAction;
import platform.common.bo.Page;
import platform.common.exception.BusinessException;

/**
 * 
 * 订单
 * 
 * @author ZhangYQ
 * 
 */
public class GoodsOrderAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private IGoodsOrderService goodsOrderService;
	private IGoodsOrderDetailService goodsOrderDetailService;
	private IContractService contractService;
	private Page page; // 分页信息
	@SuppressWarnings("unchecked")
	private List wrapperList;
	private List<ContractWrapper> contractWrapperList;
	private GoodsOrderForm form;
	private ContractForm contractForm;
	private GoodsOrderConditionForm condition;
	private ContractConditionForm contractCondition;
	private List<GoodsOrderDetail> goodsOrderDetailList;
	private GoodsOrderWrapper goodsOrderWrapper;
	
	private String id;

	/**
	 * 获取所有的产品以及根据条件进行查询
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findAllGoodsOrder() throws Exception {
		page = goodsOrderService.getPageInfo(condition);
		wrapperList = goodsOrderService.findAllGoodsOrder(condition,page);
		if(condition!=null){
			return "listGoodsOrderByPage";
		}
		return "findAllGoodsOrder";
	}

	/**
	 * 初始化编辑产品页面
	 * @return
	 * @throws Exception
	 */
	public String gotoEditGoodsOrder() throws Exception {
		if(null!=id&&!"".equals(id)){
			form = goodsOrderService.getById(id);
		}
		contractWrapperList=contractService.findAllContractByStatus(contractCondition);
		return "gotoEditGoodsOrder";
	}
	/**
	 * 初始化添加
	 * @return
	 * @throws Exception
	 */
	public String gotoAddGoodsOrder() throws Exception {
		if(null!=id&&!"".equals(id)){
			contractForm = contractService.getById(id);
		}
		//contractWrapperList=contractService.findAllContractByStatus(contractCondition);
		if("1".equals(form.getOrderType())){
			return "gotoAddGoodsOrder";
		}else{
			return "gotoEditDeliveryGoodsOrder";
		}
	}
	/**
	 * 编辑产品
	 * 
	 * @return
	 * @throws Exception
	 */
	public String editGoodsOrder() throws Exception {
		try {
			//保存备货单和备货单产品明细
			goodsOrderService.editGoodsOrder(form,goodsOrderDetailList);
			if (null == form.getId()
					|| ("").equals(form.getId())) {
				this.getAjaxMessagesJson().setMessage(true, "", "新建成功");
			} else {
				this.getAjaxMessagesJson().setMessage(true, "", "编辑成功");
			}

		} catch (Exception e) {
			this.handleException(e);
		}
		return RESULT_AJAXJSON;
	}

	/**
	 * 
	 * <br/>
	 * 方法描述:分页条件查询产品结构
	 * 
	 * @return
	 * @throws Exception
	 */
	public String listGoodsOrderByPage() throws Exception {
		try {
			wrapperList = goodsOrderService.findAllGoodsOrder(condition, page);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw e;
		}
		return "listGoodsOrderByPage";
	}

	/**
	 * 初始化产品结构列表操作菜单
	 * 
	 * @return
	 * @throws Exception
	 */

	public String gotoGoodsOrderMenu() throws Exception {
		return "gotoGoodsOrderMenu";
	}

	/**
	 * 跳转到产品删除确认页面
	 */
	public String gotoDeleteGoodsOrder(){
		return "gotoDeleteGoodsOrder";
	}
	
	/**
	 * 删除产品
	 * @return
	 * @throws Exception
	 */
	public String delGoodsOrder() throws Exception {
		try {
			goodsOrderService.delGoodsOrderById(id);
			this.getAjaxMessagesJson().setMessage(true, "", "操作成功");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw e;
		}
		return RESULT_AJAXJSON;
	}
	
	/**
	 * 初始化备货单或者发货单
	 * @throws BusinessException 
	 */
	public String gotoLookGoodsOrder() throws BusinessException{
		//设置合同ID
		condition.setContractId(id);
		contractForm=contractService.getById(id);
		List<GoodsOrderWrapper> list=goodsOrderService.findAllGoodsOrderByContractId(condition);
		if(list.size()>0){
			goodsOrderWrapper=list.get(0);
			GoodsOrderDetailConditionForm godcf=new GoodsOrderDetailConditionForm();
			godcf.setGoodsOrderId(goodsOrderWrapper.getId());
			//获取订单对应的产品明细
			wrapperList = goodsOrderDetailService.findAllGoodsOrderDetailByGoodsOrderId(godcf);
		}
		//返回查看备货单
		if("1".equals(condition.getOrderType())){
			return "gotoLookGoodsOrder";
		//返回发货单
		}else{
			return "gotoLookDeliveryGoodsOrder";
		}
	}
	
	public IGoodsOrderService getGoodsOrderService() {
		return goodsOrderService;
	}

	public void setGoodsOrderService(IGoodsOrderService goodsOrderService) {
		this.goodsOrderService = goodsOrderService;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}
	@SuppressWarnings("unchecked")
	public List<GoodsOrderWrapper> getWrapperList() {
		return wrapperList;
	}

	public void setWrapperList(List<GoodsOrderWrapper> wrapperList) {
		this.wrapperList = wrapperList;
	}

	public GoodsOrderForm getForm() {
		return form;
	}

	public void setForm(GoodsOrderForm form) {
		this.form = form;
	}

	public GoodsOrderConditionForm getCondition() {
		return condition;
	}

	public void setCondition(GoodsOrderConditionForm condition) {
		this.condition = condition;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public IContractService getContractService() {
		return contractService;
	}

	public void setContractService(IContractService contractService) {
		this.contractService = contractService;
	}

	public ContractConditionForm getContractCondition() {
		return contractCondition;
	}

	public void setContractCondition(ContractConditionForm contractCondition) {
		this.contractCondition = contractCondition;
	}

	public List<ContractWrapper> getContractWrapperList() {
		return contractWrapperList;
	}

	public void setContractWrapperList(List<ContractWrapper> contractWrapperList) {
		this.contractWrapperList = contractWrapperList;
	}

	public List<GoodsOrderDetail> getGoodsOrderDetailList() {
		return goodsOrderDetailList;
	}

	public void setGoodsOrderDetailList(List<GoodsOrderDetail> goodsOrderDetailList) {
		this.goodsOrderDetailList = goodsOrderDetailList;
	}

	public GoodsOrderWrapper getGoodsOrderWrapper() {
		return goodsOrderWrapper;
	}

	public void setGoodsOrderWrapper(GoodsOrderWrapper goodsOrderWrapper) {
		this.goodsOrderWrapper = goodsOrderWrapper;
	}

	public IGoodsOrderDetailService getGoodsOrderDetailService() {
		return goodsOrderDetailService;
	}

	public void setGoodsOrderDetailService(
			IGoodsOrderDetailService goodsOrderDetailService) {
		this.goodsOrderDetailService = goodsOrderDetailService;
	}

	public ContractForm getContractForm() {
		return contractForm;
	}

	public void setContractForm(ContractForm contractForm) {
		this.contractForm = contractForm;
	}
}
